package flc.ast.adapter;

import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import flc.ast.R;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.bean.StkResBean;

public class EncyAdapter extends StkProviderMultiAdapter<StkResBean> {

    public EncyAdapter() {
        addItemProvider(new StkSingleSpanProvider(126));
        addItemProvider(new EncyProvider());
    }

    private class EncyProvider extends BaseItemProvider<StkResBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_ency;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, StkResBean bean) {
            List<StkResBean> listSp = AppSpUtils.getCollectList();
            if (listSp != null && listSp.size() != 0) {
                for (StkResBean stkResBean : listSp) {
                    if (bean.equals(stkResBean)) {
                        bean.setSelected(true);
                        break;
                    }
                }
            }
            if (baseViewHolder.getBindingAdapterPosition() % 2 == 0) {
                baseViewHolder.getView(R.id.llEncyItem).setVisibility(View.VISIBLE);
                baseViewHolder.getView(R.id.llEncyItemRight).setVisibility(View.GONE);
                Glide.with(getContext()).load(bean.getThumbUrl())
                        .into((ImageView) baseViewHolder.getView(R.id.ivEncyItemImg));
                baseViewHolder.setText(R.id.tvEncyItemTitle, bean.getName());
                baseViewHolder.setText(R.id.tvEncyItemDesc, bean.getDesc());
                String tag = "";
                for (String s : bean.getTagNameList()) {
                    tag += s + " ";
                }
                baseViewHolder.setText(R.id.tvEncyItemType, tag);
                if (bean.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivEncyItemCollect, R.drawable.ayishouc);
                } else {
                    baseViewHolder.setImageResource(R.id.ivEncyItemCollect, R.drawable.aweishouc);
                }
            } else {
                baseViewHolder.getView(R.id.llEncyItem).setVisibility(View.GONE);
                baseViewHolder.getView(R.id.llEncyItemRight).setVisibility(View.VISIBLE);
                Glide.with(getContext()).load(bean.getThumbUrl())
                        .into((ImageView) baseViewHolder.getView(R.id.ivEncyItemRightImg));
                baseViewHolder.setText(R.id.tvEncyItemRightTitle, bean.getName());
                baseViewHolder.setText(R.id.tvEncyItemRightDesc, bean.getDesc());
                String tag = "";
                for (String s : bean.getTagNameList()) {
                    tag += s + " ";
                }
                baseViewHolder.setText(R.id.tvEncyItemRightType, tag);
                if (bean.isSelected()) {
                    baseViewHolder.setImageResource(R.id.ivEncyItemRightCollect, R.drawable.ayishouc);
                } else {
                    baseViewHolder.setImageResource(R.id.ivEncyItemRightCollect, R.drawable.aweishouc);
                }
            }

        }
    }

}
